package com.gitee.ywj1352.leecode;

import javafx.util.Pair;
import org.junit.Before;
import org.junit.Test;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * @author wenjie.yang on 2019/10/11.
 */
public class TreeCode {
    private TreeNode tree;

    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }

    public int maxDepthStack(TreeNode root) {


        return 0;
    }

    @Before
    public void init() {
        TreeNode t1 = new TreeNode(1);
        TreeNode t2 = new TreeNode(2);
        TreeNode t3 = new TreeNode(3);
        TreeNode t4 = new TreeNode(4);
        TreeNode t5 = new TreeNode(5);
        TreeNode t6 = new TreeNode(6);
        TreeNode t7 = new TreeNode(7);
        t1.right = t2;
        t1.left = t3;
        t2.right = t4;
        t4.right = t5;
        t2.left = t6;
        t5.left = t7;
        tree = t1;
    }

    @Test
    public void maxDepth() {
        int i = maxDepth(tree);
        System.out.println(i);
    }

    /**
     * 翻转二叉树
     *
     * @param root
     * @return
     */
    public TreeNode invertTree(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null) return null;
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode poll = queue.poll();
            TreeNode temp = poll.left;
            poll.left = poll.right;
            poll.right = temp;
            if (poll.right != null) queue.add(poll.right);
            if (poll.left != null) queue.add(poll.left);
        }
        return root;
    }

    @Test
    public void invertTree() {
        TreeNode treeNode = invertTree(tree);
        System.out.println(treeNode);
    }

    /**
     * 返回是否含有的值
     *
     * @param root
     * @param sum
     * @return
     */
    public boolean hasPathSum(TreeNode root, int sum) {
        Pair<TreeNode,Integer> pair= new Pair<>(root,0);
        Stack<Pair<TreeNode,Integer>> stack = new Stack<>();
        if (root == null) return false;
        stack.add(pair);
        while (!stack.isEmpty()) {
            Pair<TreeNode, Integer> popStack = stack.pop();
            TreeNode poll = popStack.getKey();
            Integer s = popStack.getValue();
            //计算出全部的值
            if (poll.left == null && poll.right == null) {
                if(s + poll.val == sum){
                    return true;
                }
            } else {
                s += poll.val;
            }
            if (poll.right != null) {
                stack.add(new Pair<>(poll.right,s));
            }
            if (poll.left != null) {
                stack.add(new Pair<>(poll.left,s));
            }
        }
        return false;
    }

    public boolean hasPathSum2(TreeNode root, int sum) {
        if(root == null){
            return false;
        }
        return recerive(root, sum);
    }


    private boolean recerive(TreeNode root, int sum){
        if(root == null){
            return false;
        }

        int curValue = sum -  root.val;
        if(root.left == null && root.right == null && curValue ==0){
            return true;
        }
        return recerive(root.left, curValue) || recerive(root.right, curValue);
    }

    /**
     * 前序遍历的递推公式：
     * preOrder(r) = print r->preOrder(r->left)->preOrder(r->right)
     *
     * 中序遍历的递推公式：
     * inOrder(r) = inOrder(r->left)->print r->inOrder(r->right)
     *
     * 后序遍历的递推公式：
     * postOrder(r) = postOrder(r->left)->postOrder(r->right)->print r
     */
    public void preOrder(TreeNode tree){
        System.out.println(tree.val);
        if (tree.left!=null)
            preOrder(tree.left);
        if (tree.right!=null)
            preOrder(tree.right);
    }

    public void postOrder(TreeNode tree){
        if (tree.left!=null) {
            preOrder(tree.left);
        }
        if (tree.right!=null)
            preOrder(tree.right);
        System.out.println(tree.val);
    }
    public void inOrder(TreeNode tree){
        if (tree.left!=null) {
            preOrder(tree.left);
        }
        System.out.println(tree.val);
        if (tree.right!=null)
            preOrder(tree.right);
    }

    @Test
    public void preOrder(){
        postOrder(tree);
    }



    @Test
    public void hasPathSum() {
        TreeNode t1 = new TreeNode(1);
        TreeNode t2 = new TreeNode(-2);
        TreeNode t3 = new TreeNode(-3);
        TreeNode t4 = new TreeNode(1);
        TreeNode t5 = new TreeNode(3);
        TreeNode t6 = new TreeNode(-2);
        TreeNode t7 = new TreeNode(-1);
        t1.left = t2;
        t1.right = t3;
        t2.left = t4;
        t2.right = t5;
        t3.left = t6;
        t4.left = t7;

        boolean b = hasPathSum(t1, 2);
        System.out.println(b);
    }



    /**
     * 验证查询二叉树
     * @param root
     * @return
     */
    public boolean isValidBST(TreeNode root) {



        return false;
    }


    // Definition for a binary tree node.
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

}
